package com.cake.controller.weixin;

import com.cake.util.CheckSignatureUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.security.DigestException;

/**
 * Created by XuRui's Hands.
 * User:XuRui
 * Date:2017/1/3
 * Time:10:46
 */

@Controller
@RequestMapping("/Configure")
public class Configure {

    private String token;

    public Configure() {
        this.token ="weixin";
    }

    @ResponseBody
    @RequestMapping("checkSignature")
    public String checkSignature(@RequestParam(value="signature",required=false,defaultValue="") String signature,
                                 @RequestParam(value="timestamp",required=false,defaultValue="") String timestamp,
                                 @RequestParam(value="nonce",required=false,defaultValue="") String nonce,
                                 @RequestParam(value="echostr",required=false,defaultValue="") String echostr)
    {
        //字典排序
        String sortString = CheckSignatureUtil.dictionarySort(token, timestamp, nonce);
        try {
            //加密
            String mytoken = CheckSignatureUtil.SHA1(sortString);
            // 校验签名
            if(mytoken.equals(signature)){
                return echostr;
            }
        } catch (DigestException e) {
            System.out.println("加密失败");
        }
        return null;
    }
}
